// pages/law/detail/index.js

var WxParse = require('../../../wxParse/wxParse.js');
import util from '../../../utils/util';

function _getList(_cid, _page, _pageSize, _pageObj){
  wx.showToast({
    title: '加载中',
    icon: 'loading',
    duration: 10000
  });

  var _auth = util.genAuth();

  wx.request({
    url: `${getApp().globalData.serverHost}/wx/bd/law/list`, //仅为示例，并非真实的接口地址
    data: {
      cId: _cid,
      page: _page,
      pSize: _pageSize,
      timestamp: _auth.timestamp,
      token: _auth.token
    },
    method: "post",
    header: {
        'content-type': 'application/json'
    },
    success: function(res) {
      wx.hideToast();
      var _list = _pageObj.data.list || [];

      if(!!res && !!res.data){
        for(var j=0; j<res.data.detail.length; j++){
              res.data.detail[j].publishTime = util.formatCommonShortTime(res.data.detail[j].publishTime);
              res.data.detail[j].effectiveTime = util.formatCommonShortTime(res.data.detail[j].effectiveTime);
          }

        _list = _list.concat(res.data.detail || []);

        _pageObj.setData({
          list: _list
        });
      }
    },
    fail: function(res){
      console.log(res);
    }
  })
};

Page({
  data:{
    name: "",
    cId: 0,
    list: [],
    page: 1,
    pageSize: 30,
    scrollHeight: 100,
    scrollTop: 0
  },
  onLoad:function(options){
    var that = this;
    this.setData({
      name: options.name || "",
      parentName: options.parent || "",
      cId: options.id
    });

    var _appInstance = getApp();
    var _globalData = _appInstance.globalData;
    this.setData({
      switchForVerify: !!_globalData.switchForVerify || false
    });
    
    wx.setNavigationBarTitle({
      title: this.data.parentName + " > " + this.data.name
    });

    // 获取高度
    wx.getSystemInfo({
        success:function(res){
            that.setData({
                scrollHeight:res.windowHeight
            });
        }
    });

    // 获取列表
    _getList(options.id, this.data.page, this.data.pageSize, this);
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  loadMore: function(){
    // 获取列表
    this.setData({
      page: this.data.page+1
    });
    _getList(this.data.cId, this.data.page, this.data.pageSize, this);
  },
  scroll:function(event){
    //   该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    //  this.setData({
    //      scrollTop : event.detail.scrollTop
    //  });
  },
})